﻿using Shop.BLL;
using Shop.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.MVC.Areas.Admin.Controllers
{
    public class AdController : Controller
    {
        BaseManager<Ad> adManager=new BaseManager<Ad>();
        // GET: Admin/Ad
        public ActionResult Index()
        {
            var list=adManager.GetList();
            return View(list);
        }

        [HttpPost]
        public ActionResult Create(HttpPostedFileBase file,string href)
        {
            Ad ad = new Ad();
            string[] allows = { "jpg", "png", "gif" };
            var extName = file.FileName.Split('.')[1];
            if (allows.Contains(extName))
            {
                var name = Guid.NewGuid().ToString() + "." + extName;
                var path = "/uploads/" + name;
                file.SaveAs(Server.MapPath(path));
                ad.Picture = path;
            }

           
            ad.Href = href;
          
            adManager.Add(ad);
            return RedirectToAction("Index");
        }
    }
}